package net.huashitong.threeSeven.service;

import com.alibaba.druid.sql.visitor.functions.Trim;
import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.threeSeven.dao.IndexDataDao;
import net.huashitong.threeSeven.dao.IndexWithAreaDao;
import net.huashitong.threeSeven.dao.JdFinishDetailDao;
import net.huashitong.threeSeven.entity.IndexData;
import net.huashitong.threeSeven.entity.IndexWithArea;
import net.huashitong.threeSeven.entity.JdFinishDetail;
import net.huashitong.threeSeven.filter.RequestModelFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.huashitong.threeSeven.util.DataUtils.dataFormat;
import static net.huashitong.threeSeven.util.DataUtils.tdf;
import static org.apache.commons.lang3.StringUtils.trim;

@Service
public class IndexWithAreaService extends StringPKService<IndexWithArea> {
    @Autowired
    private IndexWithAreaDao dao;
    @Autowired
    private IndexDataDao indexDataDao;
    @Autowired
    private JdFinishDetailDao jdFinishDetailDao;
    @Autowired
    private JdFinishDeatilService jdFService;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private IndexDataService indexDataService;

    @Override
    protected BaseDao<IndexWithArea, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(IndexWithArea obj) {
        obj.setSysId(tableService.updateMaxSysId("t_gis_tsIndexWithArea", null));
    }

    //该指标全市排名
    public Map<String, Object> getIndexDetailOne(RequestModelFilter filter) {
        if (filter.getIndexCode().length() > 4) {
            filter.setIndexCode(filter.getIndexCode().substring(0, 4));
        }
        Map<String, Object> resMap = new HashMap<>();
        List<Map<String, String>> resultList = new ArrayList<>();
        String newDeadline = "";
        String indexName = "";
        List<IndexWithArea> list;
        if (ObjectUtils.isNotBlank(filter.getDateTime())) {
            newDeadline = filter.getDateTime();
            filter.setDeadline(newDeadline);
            list = dao.getIndexInArea(filter);
        } else {
            newDeadline = dao.getNewDeadline();
            filter.setDeadline(newDeadline);
            list = dao.getIndexInArea(filter);
        }

        if (list.size() == 0 || list == null) {
            return null;
        } else {
            indexName = list.get(0).getIndexName();
        }
        for (IndexWithArea indexArea : list) {
            Map<String, String> strMap = new HashMap<>();
            strMap.put("indexCode", indexArea.getIndexCode());   //指标编码
            strMap.put("indexName", indexArea.getIndexName());   //指标名称
            strMap.put("areaCode", indexArea.getAreaCode());     //地区编码
            strMap.put("areaName", indexArea.getAreaName());     //地区名称
            strMap.put("increase", trim(indexArea.getIncrease()));     //增长
            strMap.put("increaseUnit", "%");                     //增长单位
            strMap.put("total", trim(indexArea.getTotal()));     //累计
            strMap.put("totalUnit", "万元");                      //累计单位
            if (ObjectUtils.isNotBlank(indexArea.getRank())) {
                strMap.put("rank", indexArea.getRank());         //排名
            }
            resultList.add(strMap);
        }
        String ctitle = indexName + "指标全市排名情况";
        resMap.put("dateTime", newDeadline);
        resMap.put("resultList", resultList);
        resMap.put("title", ctitle);
        return resMap;
    }

    //同比增长达成目标情况
    public Map<String, Object> getIndexDetailTwo(RequestModelFilter filter) {

        String newDateTime = "";
        if (ObjectUtils.isBlank(filter.getDateTime())) {
            newDateTime = indexDataDao.getNewDateTime();
            filter.setDateTime(newDateTime);
        } else {
            newDateTime = filter.getDateTime();
        }

        IndexData indexData = indexDataDao.queryIndexFactDate(filter);
        JdFinishDetail finishDetail = jdFinishDetailDao.queryByCodeAndTime(filter);

        Map<String, Object> map = new HashMap<>();

        if (indexData == null || finishDetail == null) {
            return null;
        }
        map.put("rank", indexData.getJdRank());
        map.put("increase", indexData.getIncrease());

        map.put("factFinishIncrease", trim(finishDetail.getIncrease()));              //实际比增
        map.put("factFinishIncreaseUnit", finishDetail.getIncreaseUnit());      //实际比增单位
        map.put("increaseTarget", trim(tdf(indexData.getIncreaseTarget())));          //比增市目标
        //比增百日目标
        map.put("increaseHundredTarget", tdf(indexData.getIncreaseHundredTarget()));

        map.put("factFinishTotal", trim(finishDetail.getTotal()));                    //实际总量
        map.put("factFinishTotalUnit", finishDetail.getTotalUnit());            //实际总量单位
        map.put("totalTarget", trim(tdf(indexData.getTotalTarget())));                //总量市目标
        //总量百日目标
        map.put("totalHundredTarget", tdf(indexData.getTotalHundredTarget()));
        map.put("dateTime", newDateTime);                                       //季度时间
        return map;
    }


    //同比增长达成目标情况
    public Map<String, Object> getIndexDetailTwoInWeb(RequestModelFilter filter) {

        String newDateTime = "";
        if (ObjectUtils.isBlank(filter.getDateTime())) {
            newDateTime = indexDataDao.getNewDateTime();
            filter.setDateTime(newDateTime);
        } else {
            newDateTime = filter.getDateTime();
        }

        IndexData indexData = indexDataDao.queryIndexFactDate(filter);
        JdFinishDetail finishDetail = jdFinishDetailDao.queryByCodeAndTime(filter);

        Map<String, Object> map = new HashMap<>();

        if (indexData == null || finishDetail == null) {
            return null;
        }
        if("万元".equals(finishDetail.getTotalUnit())){
            map.put("factFinishTotalUnit", "亿元");            //实际总量单位
            map.put("factFinishTotal", dataFormat(Double.parseDouble(trim(finishDetail.getTotal())) / 10000 + ""));                    //实际总量
            map.put("totalTarget",dataFormat(Double.parseDouble(trim(tdf(indexData.getTotalTarget()))) / 10000 + ""));                //总量市目标
        }else{
            map.put("factFinishTotalUnit", finishDetail.getTotalUnit());            //实际总量单位
            map.put("factFinishTotal", trim(finishDetail.getTotal()));                    //实际总量
            map.put("totalTarget", trim(tdf(indexData.getTotalTarget())));                //总量市目标
        }

        map.put("rank", indexData.getJdRank());
        map.put("increase", indexData.getIncrease());
        map.put("factFinishIncrease", trim(finishDetail.getIncrease()));              //实际比增
        map.put("factFinishIncreaseUnit", finishDetail.getIncreaseUnit());      //实际比增单位
        map.put("increaseTarget", trim(tdf(indexData.getIncreaseTarget())));          //比增市目标
        //比增百日目标
        map.put("increaseHundredTarget", tdf(indexData.getIncreaseHundredTarget()));

        //总量百日目标
        map.put("totalHundredTarget", tdf(indexData.getTotalHundredTarget()));
        map.put("dateTime", newDateTime);                                       //季度时间
        return map;
    }

    //指标各月情况
    public Map<String, Object> getIndexDetailThree(RequestModelFilter filter) {
        Map<String, Object> resMap = new HashMap<>();
        List<JdFinishDetail> list = jdFinishDetailDao.queryByCode(filter);

        List<Map<String, String>> resList = new ArrayList<>();

        for (JdFinishDetail f : list) {
            Map<String, String> map = new HashMap<>();
            map.put("indexCode", f.getIndexCode());
            map.put("jdTime", f.getJdTime());
            map.put("total", trim(f.getTotal()));
            map.put("totalUnit", f.getTotalUnit());
            map.put("increase", trim(f.getIncrease()));
            map.put("increaseUnit", f.getIncreaseUnit());
            map.put("indexName", f.getIndexName());
            resList.add(map);
        }
        resMap.put("resultList", resList);
        return resMap;
    }


    public Map<String, Object> queryThirdGrade(RequestModelFilter filter) {
        if ("1".equals(filter.getType())) {
            if (filter.getIndexCode().length() > 4) {
                filter.setIndexCode(filter.getIndexCode().substring(0, 4));
            }
            String newDeadline = filter.getDateTime();
            filter.setDeadline(newDeadline);

            Map<String, Object> map32 = new HashMap<>();
            List<Map<String, String>> secTitle = new ArrayList<Map<String, String>>();
            List<IndexWithArea> list = dao.getIndexInArea(filter);
            Map<String, String> map0 = new HashMap<>();
            Map<String, String> map00 = new HashMap<>();
            if (list != null && list.size() > 0) {
                List<Map<String,Object>> mapList = new ArrayList<>();
                map32.put("firTitle","地区");
                map32.put("title",list.get(0).getIndexCode()+"各地区情况");
                String totalUnit = "万元";
                map0.put("column", "地区完成额("+totalUnit+")");
                secTitle.add(map0);
                String increaseUnit = "%";
                map00.put("column", "地区增长率("+increaseUnit+")");
                secTitle.add(map00);
                map32.put("secTitle",secTitle);
                for(IndexWithArea jd : list){
                    Map<String,Object> map = new HashMap<>();
                    List<Map<String,Object>> list0 = new ArrayList<>();
                    Map<String,Object> map1 = new HashMap<>();
                    Map<String,Object> map2 = new HashMap<>();
                    map.put("index","0");
                    map1.put("rightValue",trim(jd.getTotal()));
                    map1.put("index","0");
                    list0.add(map1);
                    map2.put("rightValue",trim(jd.getIncrease()));
                    map2.put("index","0");
                    list0.add(map2);
                    map.put("rightdata",list0);
                    map.put("leftValue",jd.getAreaName());
                    mapList.add(map);
                }
                map32.put("data",mapList);
            }
            return map32;
        }else{
            Map<String, Object> map32 = new HashMap<>();
            List<Map<String, String>> secTitle = new ArrayList<Map<String, String>>();
            List<JdFinishDetail> list = jdFinishDetailDao.queryByCode(filter);
            Map<String, String> map0 = new HashMap<>();
            Map<String, String> map00 = new HashMap<>();
            if (list != null && list.size() > 0) {
                List<Map<String,Object>> mapList = new ArrayList<>();
                map32.put("firTitle","季度");
                map32.put("title",list.get(0).getIndexName()+"各季度情况");
                String totalUnit = list.get(0).getTotalUnit();
                map0.put("column", "当季总量("+totalUnit+")");
                secTitle.add(map0);
                String increaseUnit = list.get(0).getIncreaseUnit();
                map00.put("column", "当季比增("+increaseUnit+")");
                secTitle.add(map00);
                map32.put("secTitle",secTitle);
                for(JdFinishDetail jd : list){
                    Map<String,Object> map = new HashMap<>();
                    List<Map<String,Object>> list0 = new ArrayList<>();
                    Map<String,Object> map1 = new HashMap<>();
                    Map<String,Object> map2 = new HashMap<>();
                    map.put("index","0");
                    map1.put("rightValue",trim(jd.getTotal()));
                    map1.put("index","0");
                    list0.add(map1);
                    map2.put("rightValue",trim(jd.getIncrease()));
                    map2.put("index","0");
                    list0.add(map2);
                    map.put("rightdata",list0);
                    map.put("leftValue",jd.getJdTime());
                    mapList.add(map);
                }
                map32.put("data",mapList);
            }
            return map32;

        }
    }

}
